﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Utilities.Menu.UIElements
{
    public class ScrollingTextBox
    {
        Rectangle drawing_rect, up_arrow_rect, down_arrow_rect; 
        SpriteFont box_font;
        Texture2D up_arrow, down_arrow, box;
        ArrayList<string> lines;

        int first_line, nOfDiplayedLines, offset;

        public ScrollingTextBox(Rectangle drawing_rect, SpriteFont box_font, Texture2D up_arrow, Texture2D down_arrow, Texture2D box, int offset)
        {
            this.drawing_rect = drawing_rect;
            this.box_font = box_font;
            this.up_arrow = up_arrow;
            this.down_arrow = down_arrow;
            this.box = box;
            this.offset = offset;

            var gdViewport = box.GraphicsDevice.Viewport;

            up_arrow_rect = new Rectangle(gdViewport.Width / 2 - up_arrow.Width / 2, drawing_rect.Top - 20 - up_arrow.Height, up_arrow.Width, up_arrow.Height);
            down_arrow_rect = new Rectangle(gdViewport.Width / 2 - down_arrow.Width / 2, drawing_rect.Bottom + 20, down_arrow.Width, down_arrow.Height);

            lines = new ArrayList<string>(20);
            first_line = 0;

            var sm = box_font.MeasureString("AAAAAAAAAAAA");

            nOfDiplayedLines = (int)Math.Round((box.Height - 20 * 2.0f) / (float)offset);            
        }

        public void AddLine(string line)
        {
            lines.Add(line);
        }

        public void RemoveLine(string line)
        {
            lines.Remove(line);
        }

        public void RemoveLine(int index)
        {
            lines.RemoveAt(index);
        }

        public bool UpArrowPressed()
        {
            if (first_line - 1 >= 0)
            {
                first_line -= 1;
                return true;
            }

            return false;
        }

        public bool DownArrowPressed()
        {
            if (first_line + 1 < lines.Count && (first_line + 1) + nOfDiplayedLines < lines.Count)
            {
                first_line += 1;
                return true;
            }

            return false;
        }

        public void Draw(SpriteBatch spriteBatch, float alpha)
        {
            spriteBatch.Draw(box, drawing_rect, Color.White * alpha);
            spriteBatch.Draw(up_arrow, up_arrow_rect, Color.White * alpha);

            DrawBoxEntry(spriteBatch, alpha);

            spriteBatch.Draw(down_arrow, down_arrow_rect, Color.White * alpha);
        }

        private void DrawBoxEntry(SpriteBatch spriteBatch, float alpha)
        {
            var gdViewport = spriteBatch.GraphicsDevice.Viewport;

            for (int i = first_line; i < (first_line + nOfDiplayedLines < lines.Count ? first_line + nOfDiplayedLines : lines.Count) ; i++)
            {
                var sm = box_font.MeasureString(lines[i]);
                spriteBatch.DrawString(box_font, lines[i], new Vector2(gdViewport.Width / 2 -(int)(sm.X / 2.0f),drawing_rect.Top + 20 + (i - first_line) * offset), Color.White * alpha);
            }
        }

        public Rectangle DownArrowCollisionBox
        {
            get { return down_arrow_rect; }
        }

        public Rectangle UpArrowCollisionBox
        {
            get { return up_arrow_rect; }
        }
    }
}
